<template>
	<view>
		<view class="bg" style="height: 560rpx;">
			<view @tap="login">
				<view class="avatar">
					<image :src="userInfo.avatar"></image>
				</view>
				<view class="nickname">{{userInfo.nickname}}</view>
			</view>
		</view>
		<view class="content">
			<view @tap="toShoucang">
				<view class="icon">
					<image src="/static/img/shoucang_my.png"></image>
				</view>
				<view class="shoucang">我的收藏</view>
				<view class="arrow">
					<image src="/static/img/rightarrow.png"></image>
				</view>
			</view>
			<view @tap="toMyjipu">
				<view class="icon">
					<image src="/static/img/jipu.png"></image>
				</view>
				<view class="shoucang">我的记谱</view>
				<view class="arrow">
					<image src="/static/img/rightarrow.png"></image>
				</view>
			</view>
			<view @tap='toSet'>
				<view class="icon">
					<image src="/static/img/set.png"></image>
				</view>
				<view class="shoucang">我的设置</view>
				<view class="arrow">
					<image src="/static/img/rightarrow.png"></image>
				</view>
			</view>
			<view>
				<view class="icon">
					<image src="/static/img/zaixiankefu.png"></image>
				</view>
				<view class="shoucang" @tap="toConcat">我的客服</view>
				<view class="arrow">
					<image src="/static/img/rightarrow.png"></image>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userInfo: {
					avatar: '',
					nickname: ''
				}
			}
		},
		methods: {
			// 去往其他页面
			toShoucang() {
				let url = '/pages/shoucang/shoucang'
				this.$cantoPage(url)
			},
			toMyjipu() {
				let url = '/pages/myjipu/myjipu'
				this.$cantoPage(url)
			},
			toSet() {
				let url = '/pages/set/set'
				this.$cantoPage(url)
			},
			toConcat() {
				let url = '/pages/concat/concat'
				this.$cantoPage(url)
			},

			async login() {
				if(!uni.getStorageSync('info')){
				let ret = await this.$request({
					url: '/user/getCode'
				})
				console.log(ret)
				window.location.href = ret
				}
			},
			getuserinfo() {
				let infoStorage = uni.getStorageSync('info')

				if (infoStorage) {
					let info = JSON.parse(infoStorage)
					this.userInfo = {
						avatar: info.headimgurl,
						nickname: info.nickname
					}
				} else {
					console.log(infoStorage, 'infoStorage');
					this.userInfo = {
						avatar: "/static/img/nothing.png",
						nickname: "点击登录"
					}
				}
			},
		},
		onLoad() {
			this.getuserinfo()
		},
		async onShow() {
			let url = window.location.search
			if (url) {
				url = url.slice(1)
				if (url.indexOf('code=') !== -1) {
					const params = new URLSearchParams(url);
					let code = params.get('code')
					let ret = await this.$request({
						url: '/user/getToken',
						data: {
							code
						}
					})
					const str = JSON.stringify(ret)
					console.log(str, 'str');
					uni.setStorageSync('info', str)
					window.location.href="http://192.168.43.224:8080/#/pages/my/index"
				}
			}
					this.getuserinfo()

		}
	}
</script>

<style>
	.bg>view {
		position: absolute;
		top: 200rpx;
		left: 50%;
		transform: translateX(-50%);
	}

	.avatar {
		text-align: center;
	}

	.avatar image {
		width: 160rpx;
		height: 160rpx;
		border-radius: 50%;
	}

	.nickname {
		font-size: 42rpx;
		margin-top: 20rpx;
		text-align: center;
	}

	.content {
		box-sizing: border-box;
		padding: 20rpx;
	}

	.content>view {
		box-sizing: border-box;
		padding: 30rpx 0;
		display: flex;
		justify-content: space-between;
		align-items: center;
		font-size: 36rpx;
	}

	.icon {
		width: 10%;
	}

	.icon image {
		width: 55rpx;
		height: 55rpx;
		vertical-align: middle;
	}

	.shoucang {
		width: 75% !important;
		background-color: #fff;
		text-align: left;
	}

	.arrow {
		width: 10%
	}

	.arrow image {
		width: 45rpx;
		height: 45rpx;
	}
</style>
